package epoverty;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

public class AccountsSummary extends JPanel
{
    //Fields
    private EPovertyMain parentFrame;
    private JTabbedPane tabbedPane;
    private int tabNumber;
    private String accountsQuery = String.format(
            "SELECT %s, %s, %s, %s FROM accounts",
            "accountName AS Account",
            "description AS Description",
            "balance AS Balance",
            "autoRedirectPercent AS 'Distribution %'");
    String[] headerToolTips =
    {
        "name of the account",
        "a short description of the account",
        "the amount of money in the account",
        "the percentage automatically taken from every donation"
    };

    //Constructor
    public AccountsSummary(JTabbedPane tabs, EPovertyMain parent)
    {
        parentFrame = parent;
        tabbedPane = tabs;
        tabNumber = tabbedPane.getTabCount();
        tabbedPane.add(buildPanel());
    }

    //Component Layout
    private BackgroundPanel buildPanel()
    {
        //Main Panel
        BackgroundPanel mainPanel = new BackgroundPanel(null, BackgroundPanel.ACTUAL, 1.0f, 0.5f);
        mainPanel.setLayout(new BorderLayout());
        GradientPaint gradient = new GradientPaint(0, 0, Color.DARK_GRAY, 100, 100, Color.DARK_GRAY);
        mainPanel.setPaint(gradient);

        //Tool Panel
        JPanel toolPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        JButton distributionButton = new JButton("Distribution Settings...");
        distributionButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                parentFrame.distributionJMI.doClick();
            }
        });

        JButton refreshButton = new JButton("Refresh");
        refreshButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                refreshView();
            }
        });

        toolPanel.add(refreshButton);
        toolPanel.add(distributionButton);
        mainPanel.add(toolPanel, BorderLayout.NORTH);

        //Table
        SortableTable accountsTable = TableMaker.createTable(accountsQuery);
        accountsTable.setHeaderToolTips(headerToolTips);
        accountsTable.setColumnWidths(0, 450, 0, 0);
        JScrollPane scrollPane = new JScrollPane(accountsTable);
        mainPanel.add(scrollPane, BorderLayout.CENTER);

        //Pie Chart
        ObjectTableModel tableModel = (ObjectTableModel) accountsTable.getModel();
        Object[] accountNames = tableModel.getColumnData(0);
        Object[] balances = tableModel.getColumnData(2);

        JPanel piePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        PieChart chart = new PieChart("Balance", accountNames, balances);
        chart.setPreferredSize(new Dimension(400, 250));
        piePanel.add(chart);
        mainPanel.add(piePanel, BorderLayout.SOUTH);

        return mainPanel;
    }

    //Refresh panel when database changes
    public void refreshView()
    {
        tabbedPane.setComponentAt(tabNumber, buildPanel());
    }
}